import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL), //可传参数，配置base路径，例如'/app'
  routes,
  scrollBehavior: (to, from, savedPosition) => {
    return savedPosition ? savedPosition : { top: 0 }
  },
})

router.beforeEach((to) => {
  document.title = to.meta.title as string
  if (to.path === '/login') {
    //在登录页做清除操作，如清除token等
  }

  // if (!localStorage.getItem('token') && to.path !== '/login' && to.meta.auth) {
  //   // 未登陆且访问的不是登录页，重定向到登录页面
  //   return { path: '/login', query: { redirect: to.fullPath }, }
  // }
})

export default router
